package com.shisfish.library.Controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.shisfish.library.Impl.PurchaseImpl;
import com.shisfish.library.Model.Purchase;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.DateFormatUtils;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;
@Path("purchase")
public class PurchaseController extends GlobalVariable {

private PurchaseImpl purchaseImpl = new PurchaseImpl();
	
	@POST
	@Path("insertAPurchase/{idadmin}/{type}")
	@Produces("application/json; charset=utf-8")
	public String insertAPurchase(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request, 
			@FormParam("supplier") int supplier, @FormParam("bookDetail") String bookDetail, 
			@FormParam("money") String money, @FormParam("buyer") String buyer, 
			@FormParam("remarks") String remarks, @PathParam("type") int type, 
			@FormParam("bookNumbers") int bookNumbers) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			
			Date date = new Date();
			String serail = DateFormatUtils.getDateformat_6(date);
			
			// 查找今天有木有相同的serail
			int count = purchaseImpl.countBySerial(serail);
			serail += "-" + (count + 1);
			
			Purchase purchase = new Purchase(serail, supplier, bookDetail, bookNumbers, 0, money, buyer, remarks, null, null, null, null);
			switch (type) {
			case 1:
				purchase.setLoanTime(date);
				purchase.setState(1);
				break;
			case 2:
				purchase.setOrder_time(date);
				purchase.setState(2);
				break;
			case 3:
				purchase.setCheck_time(date);
				purchase.setState(3);
				break;
			case 4:
				purchase.setOut_time(date);
				purchase.setState(4);
				break;

			default:
				break;
			}
			
			int flag = purchaseImpl.insertOne(purchase);
			if (flag == 0) {
				globalMap.put(STATUS_CODE, INSERT_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("updateAPurchase/{idadmin}/{type}/{id}")
	@Produces("application/json; charset=utf-8")
	public String updateAPurchase(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request, 
			@FormParam("supplier") int supplier, @FormParam("bookDetail") String bookDetail, 
			@FormParam("money") String money, @FormParam("buyer") String buyer,
			@FormParam("remarks") String remarks, @PathParam("type") int type, 
			@FormParam("bookNumbers") int bookNumbers, @PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<Purchase> purchases = purchaseImpl.selectById(id);
			
			if (purchases.size() > 0) {
				Purchase purchase = purchases.get(0);
				purchase.setBookDetail(bookDetail);
				purchase.setBookNumbers(bookNumbers);
				purchase.setMoney(money);
				purchase.setRemarks(remarks);
				purchase.setSupplier(supplier);
				purchase.setBuyer(buyer);
				Date date = new Date();
				switch (type) {
				case 1:
					purchase.setLoanTime(date);
					purchase.setState(1);
					break;
				case 2:
					purchase.setOrder_time(date);
					purchase.setState(2);
					break;
				case 3:
					purchase.setCheck_time(date);
					purchase.setState(3);
					break;
				case 4:
					purchase.setOut_time(date);
					purchase.setState(4);
					break;

				default:
					break;
				}
				
				int flag = purchaseImpl.updateOne(purchase);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, UPDATE_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("selectAllPage/{type}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectAllPage(@PathParam("type") int type, 
			@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize, 
			@FormParam("supplier") String supplier) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		int count = purchaseImpl.countByType(type, supplier);
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize); 
			List<Purchase> purchases = purchaseImpl.selectByTypePage(type, supplier, currentPage, pageSize);
			if (purchases.size() > 0) {
				globalMap.put(STATUS_CODE, SUCCESS);
				globalMap.put("page", page);
				globalMap.put("list", purchases);
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("selectById/{id}")
	@Produces("application/json; charset=utf-8")
	public String updateAPurchase(@PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
			
		List<Purchase> purchases = purchaseImpl.selectById(id);
		
		if (purchases.size() > 0) {
			globalMap.put("obj", purchases.get(0));
			globalMap.put(STATUS_CODE, SUCCESS);
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
}
